﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using simulatMeter.Lib;

namespace simulatMeter
{
    public partial class Form1 : Form
    {
        Config cfg = new Config();
        MeterControl mcontrol;
        PacketView pv;


        serialPortSetting portSetting;

        public Form1()
        {
            InitializeComponent();

            mcontrol = new MeterControl();
            mcontrol.Dock = DockStyle.Fill;
            this.tpMeters.Controls.Add(mcontrol);

            portSetting = new serialPortSetting();
            //portSetting.Dock = DockStyle.Fill;
            portSetting.Anchor = AnchorStyles.Left | AnchorStyles.Right|AnchorStyles.Top|AnchorStyles.Bottom;
            this.tpPort.Controls.Add(portSetting);
            portSetting.Location = new Point(+ this.Location.X+this.tpPort.Size.Width/3,this.Location.Y+ this.tpPort.Height/3);


            pv = new PacketView();
            pv.Dock = DockStyle.Fill;
            this.tpMessage.Controls.Add(pv);

            portSetting.onPacketReceive += portSetting_onPacketReceive;
            portSetting.onPacketSend += portSetting_onPacketSend;

        }


        //数据发送事件
        void portSetting_onPacketSend(object sender, Ulitily.PacketArgs args)
        {

            //////////////////////////////////////////////////////////////////////////
            //显示处理
            pv.PacketSend(sender, args);
        }


        //数据接收事件
        void portSetting_onPacketReceive(object sender, Ulitily.PacketArgs args)
        {
            //////////////////////////////////////////////////////////////////////////
            //显示处理
            pv.PacketGet(sender, args);

            //////////////////////////////////////////////////////////////////////////
            //地址匹配，支持AA

            //////////////////////////////////////////////////////////////////////////
            //命令码
            if (args.packet.code.c != CodeStyle.Read97 && args.packet.code.c != CodeStyle.Read ) return;
            if (args.packet.code.dir != DIRECTION.Slater )return ;

            byte[] rawdata = args.packet.dataRaw;
            if(rawdata==null)return;


            //////////////////////////////////////////////////////////////////////////
            //数据项
            UInt32 di = Ulitily.ShareClass.data2Di(rawdata,(rawdata.Length>=4)?4:2);


            //////////////////////////////////////////////////////////////////////////
            //从cfg中，读取对应的数据
            if (mcontrol.cfg.clist.Count <= 0) return;
            else
            {
                foreach(ConfigItem it in mcontrol.cfg.clist){

                    //usage 属性,配置不使用，跳过
                    if (it.Usage == false) continue;

                    //找表
                    List<UInt64> mtrList = it.GetMeters(args.packet.meterNumberHex);
                   // if (it.mlist.Count > 0 && mtrList == null) continue;

                    //读取di数据
                    MeterDiData mdd = it.GetMeterDiData(di);

                    //搜表操作: 返回乱码
                    if (mtrList != null && mtrList.Count>1)
                    {
                        byte[] luanMa = new byte[] { 0xA8,0x01,0x02,0x03,0x04,0x05,0x06,0x58,0xaa,0xbb,0xcc};
                        portSetting.write(luanMa,0,luanMa.Length,1000);
                        pv.CommDisplay(this, "-> :", luanMa, 0, luanMa.Length);
                        return;
                    }
                    //无电表
                    if(mtrList==null ||mtrList.Count ==0)return;

                    //组装数据包
                    byte[] bytepacke = args.packet.toBytes();
                    MeterPacket rp = new MeterPacket(bytepacke, 0, bytepacke.Length);
                    rp.code = new Code(0x81);
                    rp.meterNumberHex = mtrList[0];



                    if(mdd == null)
                    {
                        //////////////////////////////////////////////////////////////////////////
                        //读表号
                        if (di == 0x04000401)
                        {
                            byte[] alldata = new byte[] { 0x01, 0x04, 0x00, 0x04 ,0,0,0,0,0,0};
                            Ulitily.ShareClass.UInt48ToBytes(mtrList[0], alldata, 4, 0);
                            rp.dataRaw = alldata;
                        }
                        else
                        {
                        //////////////////////////////////////////////////////////////////////////
                        //数据不存在返回 0x02                          
                        byte[] alldata = new byte[] { 0x02 };
                        rp.dataRaw = alldata;
                        }

                    }else{
                        //////////////////////////////////////////////////////////////////////////
                        //数据存在，返回数据
                        byte[] rdata = mdd.diData.DataToBytes;
                        byte[] alldata = new byte[mdd.di.Length + rdata.Length];
                        Array.Copy(mdd.di, 0, alldata, 0, mdd.di.Length);
                        Array.Copy(rdata, 0, alldata, mdd.di.Length, rdata.Length);
                        rp.dataRaw = alldata;
                    }

                    portSetting.WritePacket(rp);
                    return;

                }
            }
            
        }



        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            if(portSetting.serialThread != null)portSetting.serialThread.Abort();
        }

    }
}
